<?php
class it_userid_model extends MY_Model {
	var $model = 'ituserid';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.*, 
							(CASE NVL(a.status,'0') WHEN '1' THEN 'Active' ELSE 'Suspend' END) status_name
					FROM	it_userid a 
							LEFT OUTER JOIN sw_person b ON a.user_id = b.user_id
					WHERE	UPPER(a.user_id) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.user_name) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.description) LIKE '%".strtoupper(post('keyword'))."%'
					ORDER BY ".(post('sort')=='' ? 'a.user_id' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('it_userid', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('it_userid', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_userid){
		$result = $this->db->get_where('it_userid', array('user_id' => $as_userid));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_userid)
	{
		$result	= $this->db->query("SELECT	A.*, B.NIP
                                    FROM    IT_USERID A
                                            LEFT JOIN SW_PERSON B ON A.USER_ID = B.USER_ID
									WHERE	A.USER_ID = '".$as_userid."'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function DeleteSingle($arr_rule)
	{
		try {
			$this->db->delete('it_user_role', array('user_id' => $arr_rule['user_id']));
			$this->db->delete('it_user_application', array('user_id' => $arr_rule['user_id']));
			$this->db->delete('it_userid', array('user_id' => $arr_rule['user_id']));
			return '';
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>